<template>
  <view>
    <!-- <my-goods></my-goods> -->
    <view class="goods-list">
      <view v-for="(item,index) in goodsList" :key="index" @click="goToDeatil(item)">
        <!-- 为 my-goods 组件动态绑定 goods 属性的值 -->
        <!-- <my-goods-one hello="jason" :goods="item"></my-goods-one> -->
        <view class="goods-item">
          <!-- 商品左侧区域 -->
          <view class="goods-item-left">
            <image :src="item.goods_small_logo || defaultImg" mode=""></image>
          </view>
          <!-- 商品右侧区域 -->
          <view class="goods-item-right">
            <!-- 商品标题 -->
            <view class="goods-info-title">{{item.goods_name}}</view>
            <!-- 商品价格 -->
            <view class="goods-info-box">
              <view class="goods-price">
                ￥{{item.goods_price|tofixed}}
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 查询数组
        queryObj: {
          // 查询关键词
          query: '',
          // 分类ID
          cid: '',
          // 页数索引
          pagenum: 1,
          // 每页长度
          pagesize: 10
        },
        // 商品列表的数据
        goodsList: [],
        // 总数量,用来实现分页
        total: 0,
        // 图片不显示时，默认显示的图片
        defaultImg: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
        // 节流阀
        isLoading: false
      }
    },
    filters: {
      // 把数字处理为带两位小数点的数字
      tofixed(Num) {
        return Number(Num).toFixed(2);
      }
    },
    onLoad(option) {
      this.queryObj.cid = option.cid || '',
        this.queryObj.query = option.query || '',
        // 调用请求获取商品列表
        this.getGoodsList()
    },
    methods: {
      async getGoodsList(cb) {
        // 请求之前开启节流阀
        this.isLoading = true;
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj);
        // 请求完毕关闭节流阀
        this.isLoading = false;

        // 只要数据请求完毕，就立即按需调用 cb 回调函数
        cb && cb()

        console.log(res)
        if (res.meta.status != 200) return uni.$showMsg();

        this.goodsList = [...this.goodsList, ...res.message.goods];
        this.total = res.message.total
      },
      onReachBottom() {
        // 判断是否还有下一页数据
        if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕！')

        // 判断节流阀是否打开
        if (this.isLoading) return false

        this.queryObj.pagenum += 1;
        this.getGoodsList()
      },
      // 下拉刷新
      onPullDownRefresh() {
        // 1.重置关键数据
        this.queryObj.pagenum = 1;
        this.total = 0;
        this.loading = false;
        this.goodsList = [];

        // 2.重新发起请求
        this.getGoodsList(() => uni.stopPullDownRefresh())
      },
      // 跳转到详情页
      goToDeatil(item) {
        uni.navigateTo({url:"/subpkg/goods_detail/goods_detail?goods_id=" + item.goods_id})
      }
    }
  }
</script>

<style lang="scss">
  .goods-item {
    display: flex;
    padding: 8px 1px;
    border-bottom: 1px solid #afafaf;

    .goods-item-left {
      margin-right: 10px;

      image {
        width: 100px;
        height: 100px;
        display: block;
      }
    }

    .goods-item-right {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      // align-items: center;

      .goods-info-title {
        font-size: 13px;
      }

      .goods-info-box {
        .goods-price {
          font-size: 16px;
          color: #C00000;
        }
      }
    }
  }
</style>
